<?php

declare(strict_types=1);

namespace App\Factory\Wechat;

use EasyWeChat\OpenPlatform\Application as OpenPlatformApplication;
use EasyWeChat\OfficialAccount\Contracts\Application as ApplicationInterface;
use EasyWeChat\MiniApp\Application as MiniApplication;
use EasyWeChat\OfficialAccount\Application as OfficialAccountApplication;
use EasyWeChat\Pay\Application as PayApplication;
use Hyperf\Contract\ContainerInterface;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Context\ApplicationContext;
use Psr\SimpleCache\CacheInterface;
use Pengxuxu\HyperfWechat\EasyWechat;
use function Hyperf\Config\config;

class Factory {

    public function __construct(
        protected ContainerInterface $container
    )
    {}

    /**
     * 微信公众平台
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function official()
    {

        $config = config('cloud.wechat');
        $application = new OfficialAccountApplication($config);

        return $this->init($application);
    }

    /**
     * 微信开放平台-网站应用
     * @return ApplicationInterface|OpenPlatformApplication|mixed
     */
    public function open()
    {
        $config = config('cloud.open');
        $application = new OpenPlatformApplication($config);

        return $this->init($application);
    }

    /**
     * 开放平台-第三方平台
     * @return ApplicationInterface|OpenPlatformApplication
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function third()
    {
        $config = config('cloud.open_third');
        $application = new OpenPlatformApplication($config);

        return $this->init($application);
    }

    /**
     * 小程序
     * @return MiniApplication|ApplicationInterface
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function mini():MiniApplication
    {
        $key = 'cloud.mini';
        $app = getApp();
        if (in_array($app, [])) {
            $key = "cloud.{$app}_mini";
        }
        $config = config($key);
        return EasyWechat::miniApp('default', $config);
    }

    /**
     * 微信支付
     * @return PayApplication
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function pay():PayApplication
    {
        $key = 'cloud.mini';
        $config = config($key);
        $application = new PayApplication($config);

        return $this->init($application);
    }

    /**
     * 初始化
     * @param $application
     * @return mixed
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function init($application)
    {
        $request = ApplicationContext::getContainer()->get(RequestInterface::class);
        $application->setRequest($request);

        $cache = ApplicationContext::getContainer()->get(CacheInterface::class);
        $application->setCache($cache);

        return $application;
    }
}